//
//  SENetworkReachabilityTool.m
//  kuaiyi
//
//  Created by Alphts on 16/7/8.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SENetworkReachabilityTool.h"

@implementation SENetworkReachabilityTool


+ (void)checkNetworkWithWifiBlock:(void(^)())wifiBlock wwanBlock:(void(^)())wwanBlock unReachableBlock:(void(^)())unReachableBlock unknownBlock:(void(^)())unknownBlock{
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                
                if (unknownBlock) {
                    unknownBlock();
                }
                
                break;
            case AFNetworkReachabilityStatusNotReachable:
                
                if (unReachableBlock) {
                    unReachableBlock();
                }
                
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                
                if (wwanBlock) {
                    wwanBlock();
                }
                
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                
                if (wifiBlock) {
                    wifiBlock();
                }
                
                break;
            default:
                break;
        }
        
        [SENetworkReachabilityTool stopCheck];
    }];
    
    //开启检测
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
}


+ (void)checkNetworkWithReachableBlock:(void (^)())reachableBlock unReachableBlock:(void (^)())unReachableBlock{
    
    [self checkNetworkWithWifiBlock:reachableBlock wwanBlock:reachableBlock unReachableBlock:unReachableBlock unknownBlock:unReachableBlock];    
    
}


+ (void)checkNetworkWithReachableBlock:(void (^)())reachableBlock{
    
    [self checkNetworkWithReachableBlock:^{
        
        [SVProgressHUD show];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        reachableBlock();
        
    } unReachableBlock:^{
       
        [SVProgressHUD showErrorWithStatus:@"没有网络连接"];
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        [SVProgressHUD setFadeInAnimationDuration:0];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];
    }];
}


+ (void)stopCheck{
    
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
}

@end
